<template>
	<view>
		<view class="u-demo-block__content" style="margin: 15rpx;">
			<view class="u-page__tag-item">
				<u-search placeholder="输入物料信息" 
				v-model="bomInfoParam" 
				@custom="searchWaitingCompletionList"
				@search="searchWaitingCompletionList"></u-search>
			</view>
		</view>
		<view>
			<u-tabs
				@click="tabClick"
				:list="tabList"
				:current="currentTabIndex"
				lineWidth="37"
				lineColor="#f56c6c"
				:activeStyle="{
					color: '#303133',
					fontWeight: 'bold',
					transform: 'scale(1.05)'
				}"
				:inactiveStyle="{
					color: '#606266',
					transform: 'scale(1)'
				}"
				itemStyle="padding-left: 15px; padding-right: 15px; height: 40px;">
			</u-tabs>
		</view>
		
		<view :key="index" v-for="(_workOrderList,index) in workOrderDoubleList">
			<view class="card-container" v-if="index === currentTabIndex">
				<view class="card-wrap" v-if="_workOrderList.length > 0">
					<view class="card-item" v-if="groupType != 'contractCode'">
						工单号：
						<u--text :text="workerorderCode" type="info"></u--text>
					</view>
					<view class="card-item">
						订单合同：
						<u--text :text="contractCode" style="color: aquamarine;"></u--text>
					</view>
					<view class="card-item">
						报工班组：<u-text :text="`${teamInfo ? teamInfo.deptName : teamId}`" bgColor="#e3e4e6"></u-text>
					</view>
					<view class="card-item">
						<u--text type="primary" prefixIcon="/static/submit/number.png" size="16" 
						:text="`订单总数: ${workorderNum}`"></u--text>
						<u--text color="#1ee684"  prefixIcon="/static/submit/finished.png" size="16" 
						:text="`累计完工数: ${hasCompletionNum?hasCompletionNum:0}`"></u--text>
					</view>
					<!-- 产品列表（主表） -->
					<view style="margin-top: 6px;">
							<uni-table ref="table" :loading="detailLoading"
								border
								stripe 
								type="selection" 
								@selection-change="selectionChange"
								emptyText="暂无更多数据">
							<uni-tr style="background-color: #dbfbdb;">
								<uni-th align="left" width="5" style="font-size: 12px;color: #62c188;">序号</uni-th>
								<uni-th align="left" style="font-size: 12px;color: #62c188;">不良品数</uni-th>
								<uni-th align="left" style="font-size: 12px;color: #62c188;">当天合格数</uni-th>
								<uni-th align="left" width="40" style="font-size: 12px;color: #62c188;">产地</uni-th>
								<uni-th align="left" width="100" style="font-size: 12px;color: #62c188;">产品名称</uni-th>
								<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">产品规格</uni-th>
								<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">业务状态</uni-th>
								<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">领料状态</uni-th>
								<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">订单数量</uni-th>
								<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">当天累计合格数量</uni-th>
								<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">当天累计不良品数量</uni-th>
								<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">累计完工数量</uni-th>
								<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">还欠数量</uni-th>
								<uni-th align="left" width="50" style="font-size: 12px;color: #62c188;">产品编码</uni-th>
								<uni-th align="left" width="200" style="font-size: 12px;color: #62c188;">当天备注</uni-th>
							</uni-tr>
							<uni-tr v-for="(productItem, subIndex) in _workOrderList" :key="subIndex">
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,0)">{{ subIndex+1 }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,1)">
									<uni-easyinput 
									  type="number"
									  v-model="productItem.ungoodNumReq"
									  :disabled="inputNotAavailable(productItem)"
									  @blur="ungoodNumReqChange(productItem)"
									  placeholder="不良品数" />
								</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;">
									<uni-easyinput 
									 type="number"
									  v-model="productItem.todayCompletionReq"
									  :disabled="inputNotAavailable(productItem)"
									  @blur="todayRecvNumChange(productItem)"
									  @clear="clearRecvNum(productItem)"
									  placeholder="当天合格数" />
								</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,2)">{{ productItem.prdFactoryLabel }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,3)">{{ productItem.bomName }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,4)">{{ productItem.bomStandard }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,5)">{{ productItem.kingStatusDesc }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,6)">{{ productItem.kingPickStatusDesc }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,7)">{{ productItem.workorderNum }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,8)">{{ productItem.todayCompletion }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,9)">{{ productItem.ungoodNum }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,10)">{{ productItem.hasCompletionNum }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,11)">{{ productItem.remainingCompletionNum }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,12)">{{ productItem.bomCode }}</uni-td>
								<uni-td align="left" style="font-size: 12px;text-align: left;" :style="getTdStyle(productItem,13)">
									<uni-easyinput
									  :disabled="inputNotAavailable(productItem)"
									  v-model="productItem.remark" 
									  placeholder="当天备注" />
								</uni-td>
							</uni-tr>
							</uni-table>
							<view style="padding-top: 20rpx;">
								<uni-pagination 
									prevText="前一页"
									nextText="下一页"
									:page-size="pageSize" 
									:current="pageCurrent" 
									:total="total"
									@change="pageChange"/>
							</view>
						</view>
				</view>
				<view v-else style="padding-top: 60px;">
					<u-loading-icon text="数据加载中" textSize="18" color="#19be6b" :show="loadingShow"></u-loading-icon>
					<u-empty mode="data" v-if="!loadingShow"></u-empty>
				</view>
			</view>
			
		</view>
			
		<!-- 库存列表数据 -->
		<u-popup :show="submitConfirmShow" mode="top"  @close="submitConfirmClose" :closeOnClickOverlay="false">
			<view style="max-height: 590rpx; overflow-y: auto;">
				<uni-table ref="table" :loading="submitConfirmLoading"
					border
					stripe 
					type="" 
					emptyText="暂无更多数据">
				<uni-tr style="background-color: #dbfbdb;">
					<uni-th align="left" width="5" style="font-size: 12px;color: #62c188;">序号</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">物料编码</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">物料名称</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">规格</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">当天合格数</uni-th>
					<uni-th align="left" width="60" style="font-size: 12px;color: #62c188;">当天不良品数</uni-th>
					<uni-th align="left" style="font-size: 12px;color: #62c188;">备注</uni-th>
				</uni-tr>
				
				<uni-tr v-for="(item, index) in completionWorkList" :key="index">
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ index+1 }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.bomCode }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.bomName }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.bomStandard }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.todayCompletionReq }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.ungoodNumReq }}</uni-td>
					<uni-td align="left" style="font-size: 12px;text-align: left;">{{ item.remark }}</uni-td>
				</uni-tr>
				</uni-table>
			</view>
			<view style="padding-bottom: 14rpx;">
				<uni-section title="完工日期" type="line">
					<uni-datetime-picker type="date" v-model="submitFinishDate" :clear-icon="false" @maskClick="maskClick" />
				</uni-section>
				<uni-section title="生产异常" type="line" padding>
					<uni-easyinput type="text" v-model="errMessage" placeholder="请输入生产异常信息"></uni-easyinput>
				</uni-section>
				<u-row>
					<u-col span="6">
						<u-button
							type="error"
							text="关闭"
							shape="circle" 
							customStyle="width: 260rpx;"
							@click="submitConfirmShow = !submitConfirmShow"
						></u-button>
					</u-col>
					<u-col span="6">
						<u-button
							type="primary"
							text="提交"
							shape="circle" 
							customStyle="width: 260rpx;"
							@click="submitReally"
						></u-button>
					</u-col>
				</u-row>
			</view>
		</u-popup>
		
		<!-- <u-calendar 
			@confirm="calendarConfirm"
			@close="calendarClose"
			title="已报工日期选择查询"
			:show="showDate" 
			mode="multiple" 
			monthNum="5" 
			:minDate="minDate" 
			:defaultDate="defaultDateMultiple">
		</u-calendar> -->
		
		<u-toast ref="uToast" duration="3000" />
		
		<uni-fab ref="fab"
			:pattern="pattern" 
			:content="content" 
			:horizontal="horizontal" 
			:vertical="vertical"
			:direction="direction" 
			@trigger="trigger" 
			@fabClick="fabClick" />
			
		<u-modal :show="noStationModalShow" 
			title="告警提示" content='该班组下没有配置站点,请先配置站点再操作!' 
			@confirm="noStationModalConfirm"></u-modal>
			
		<u-notify ref="uNotify"></u-notify>
	</view>
</template>

<script>
const d = new Date();
const year = d.getFullYear();
let month = d.getMonth() + 1;
month = month < 10 ? `0${month}` : month;
const date = d.getDate();

import * as submitApi from '@/api/work/worksubmit';
import * as BaseApi from '@/api/base/base';
import storage from '@/utils/storage';
import * as userApi from '@/api/user';

export default {
	components: {
	},
	data() {
		return {
			// 上一个页面传入的参数
			contractCode: "",
			workerorderCode: "",
			
			// 当前 tab 动态计算
			workorderNum:  0,
			hasCompletionNum: 0,
			
			submitMinDate: `${year}-${month}-${date-6}`,
			submitFinishDate: `${year}-${month}-${date}`,
			
			// 所有动态数组
			workOrderDoubleList: [],
			
			// 悬浮按钮开始
			pattern: {
				color: '#7A7E83',
				backgroundColor: '#fff',
				selectedColor: '#007AFF',
				buttonColor: '#007AFF',
				iconColor: '#fff',
				icon: 'gear'
			},
			content: [{
					iconPath: '/static/picking/refresh.png',
					selectedIconPath: '/static/picked/refresh.png',
					text: '刷新当前',
					active: false
				},{
					iconPath: '/static/submit/selfhistory.png',
					selectedIconPath: '/static/submit/selfhistory.png',
					text: '工单历史',
					active: false
				},
				{
					iconPath: '/static/submit/goback.png',
					selectedIconPath: '/static/submit/goback.png',
					text: '返回上页',
					active: false
				},
				{
					iconPath: '/static/picking/submit.png',
					selectedIconPath: '/static/picking/submit_selected.png',
					text: '提交报工',
					active: false
				}
			],
			horizontal: 'right',
			vertical: 'bottom',
			direction: 'vertical',
			// 悬浮按钮结束
			
			workOrderList: [],
			completionWorkList: [],
			
			loadingShow: false,
			submitInfoShow: false,
			submitTotal: 0,
			submitUngoodTotal: 0,
			tabList: [],
			show: false,
			
			// 查询参数，比如：合同号
			bomInfoParam: "",
			
			submitDateList: [],
			checked: ["选中"],
			// u-checkbox-group的v-model绑定的值如果设置为某个checkbox的name，就会被默认选中
			checkboxValue1: [],
			selectedCard: null, // 选中的卡片ID
			trueValue: true,
			// 日期弹窗
			showDate: false,
			minDate: `${year}-${month}-${date-30}`,
			defaultDateMultiple: [],
			
			isVisible: true,
			clickCount: 0,
			
			queryDateBtnText: "择查询日期",
			detailLoading: false,
			multiCollaspe: "",
			
			currentTabIndex: 0,
			currentSelectedList: [],
			
			currentCollapseIndex: null,
			
			loadStatus: "loadmore",
			pageNum: 1,
			total: 0,
			totalPageCount: 0,
			
			station: null,
			
			submitConfirmLoading: false,
			submitConfirmShow: false,
			errMessage: null,
			
			// 没有站点时，必须弹出模式窗，不能继续操作
			noStationModalShow: false,
			
			// 被选中的复选框
			selectedIndexList: [],
			
			notifyData: {
				top: 4,
				message: 'notify顶部提示',
				type: 'warning',
				color: '#ffffff',
				bgColor: '',
				fontSize: 12,
				duration: 2500,
				safeAreaInsetTop: false
			},
			groupType: '',
			
			curStationOverflowSubmitFlag: false,
			
			// 每页数据量
			pageSize: 160,
			// 当前页
			pageCurrent: 1,
			// 数据总量
			total: 0,
			
			factoryId: 0,
			teamId: 0,
			teamInfo: null,
		}
	},
	onLoad (options) {
		console.log("原始参数:", options);
		// TODO 临时测试数据
		this.defaultDateMultiple.push(`${year}-${month}-${date-1}`);
		this.defaultDateMultiple.push(`${year}-${month}-${date}`);
		if (options.factoryId) {
			this.factoryId = options.factoryId;
		}
		if (options.teamId) {
			this.teamId = options.teamId;
		}
		if (options.contractCode) {
			this.contractCode = decodeURIComponent(options.contractCode);
		}
		if (options.workerorderCode) {
			this.workerorderCode = decodeURIComponent(options.workerorderCode);
		}
			
		if (options.groupType) {
			this.groupType = options.groupType;
		}
		console.log("参数转义:", this.contractCode, this.workerorderCode);
		
		this.loadTeamInfoById();
	},
	created() {
		console.log("tabIndex, contractCode", this.currentTabIndex, this.contractCode);
		this.initStationList();
	},
	methods: {
		pageChange(e) {
			this.pageCurrent = e.current;
			this.searchWaitingCompletionList();
		},
		loadTeamInfoById() {
			userApi.getTeamById({teamId: this.teamId}).then(response => {
				if (response && response.data) {
					this.teamInfo = response.data;
				}
			}).then(res => {
				console.log("test:", this.teamInfo);
			});
			
		},
		inputNotAavailable(productItem) {	
			if (productItem.kingStatus>=6 && productItem.kingPickStatus == "1") {
				return true;
			}
			if (this.curStationOverflowSubmitFlag) {
				return false;
			}
			if (productItem.remainingCompletionNum<=0) {
				return true;
			}
			return false;
		},
		getTdStyle(row, col) {
			let startStyle = 'font-size: 12px;text-align: left;';
			if (row.kingStatus >= 6 && row.kingPickStatus == "1") {
				if (col === 5) {
					return startStyle + "background-color: aquamarine;color: red;"
				}
				return startStyle + "background-color: aquamarine;";
			}
			return startStyle;
		},
		maskClick(e){
			console.log('maskClick事件:', e);
		},
		notifySuccess(message) {
			this.notifyData.type = 'success';
			this.notifyData.message = message;
			this.$refs.uNotify.show({
				...this.notifyData
			});
		},
		selectionChange(e) {
			console.log("多选事件触发:", e);
			this.selectedIndexList = e.detail.index;
			this.batchFillRemain();
		},
		batchFillRemain() {
			if (this.workOrderDoubleList.length>0) {
				let curSelectTabList = this.workOrderDoubleList[this.currentTabIndex];
				for (let i=0; i < curSelectTabList.length;i++) {
					if (this.selectedIndexList.includes(i)) {
						let remain = curSelectTabList[i].remainingCompletionNum;
						if (remain > 0) {
							curSelectTabList[i].todayCompletionReq = remain;
							this.notifySuccess("批量填写完成，请确认后提交");
						}
					} else {
						curSelectTabList[i].todayCompletionReq = null;
					}
				}
			}
		},
		noStationModalConfirm() {
			const url = "/pages/work/dayreport/worksubmitViewB";
			uni.navigateTo({ url: url });
		},
		submitConfirmClose() {
			this.submitConfirmShow = false;
		},
		initStationList() {
			BaseApi.getStationList({teamId: this.teamId}).then(res => {
				  console.log("站点列表:", res);
				  if (!res || !res.data || res.data.length == 0) {
					  this.tabList = [];
					  this.noStationModalShow = true;
					  return;
				  }
				  this.tabList = res.data.map((item) => {
					return {
					  stationId: item.stationId,
					  stationName: item.stationName,
					  name: item.stationName,
					  overflowSubmitFlag: item.overflowSubmitFlag,
					};
				  });
				  for (let i=0;i<this.tabList.length;i++) {
					  this.tabList[i].index = i;
					  this.workOrderDoubleList[i] = [];
				  }
				  this.curStationOverflowSubmitFlag = this.tabList[0].overflowSubmitFlag;
				  this.queryWaitingCompletionList(this.tabList[0]);
			});
		},
		fabClick() {
		},
		todayRecvNumChange(productItem, e) {
			if (!productItem.todayCompletionReq) {
				return;
			}
			if (!(!isNaN(parseFloat(productItem.todayCompletionReq)) && isFinite(productItem.todayCompletionReq)) 
				|| productItem.todayCompletionReq <= 0) {
				this.$refs.uToast.error(`输入必须是大于 0 的数字`);
				return;
			}
			// if (Number(productItem.todayCompletionReq) > productItem.remainingCompletionNum) {
			// 	this.$refs.uToast.error(`${productItem.bomName}, 规格：${productItem.bomStandard} 最多还剩数量:${productItem.remainingCompletionNum} 未报工,请确认`);
			// 	productItem.todayCompletionReq = productItem.remainingCompletionNum;
			// }
		},
		ungoodNumReqChange(productItem, e) {
			if (!productItem.ungoodNumReq) {
				return;
			}
			
			if (!(!isNaN(parseFloat(productItem.ungoodNumReq)) && isFinite(productItem.ungoodNumReq)) 
				|| productItem.ungoodNumReq <= 0) {
				this.$refs.uToast.error(`输入必须是大于 0 的数字`);
				return;
			}
			if (Number(productItem.ungoodNumReq) >= productItem.workorderNum) {
				this.$refs.uToast.error(`输入不良品不可能比订单总数还多`);
				productItem.ungoodNumReq = null;
			}
		},
		clearRecvNum(row, e) {
			console.log("row->", row);
		},
		// 根据搜索条件查询
		searchWaitingCompletionList() {
			let curSelectedTab = this.tabList[this.currentTabIndex];
			this.queryWaitingCompletionList(curSelectedTab, true);
		},
		// TODO 连接后台服务
		queryWaitingCompletionList(item, isRefresh) {
			console.log("查询待报工的订单请求参数:", item);
			this.loadingShow = true;
			this.workOrderList = [];
			this.workorderNum = 0;
			this.hasCompletionNum = 0;
			submitApi.getWaitingCompletionList({
				pageNum: this.pageCurrent,
				pageSize: this.pageSize,
				factoryId: this.factoryId,
				teamIdList: [this.teamId],
				stationId: item.stationId,
				contractCode: this.contractCode,
				bomInfoParam: this.bomInfoParam,
				workerorderCode: this.workerorderCode,
				groupType: this.groupType,
			}).then(res => {
				console.log("查询待报工的订单数据成功:", res);
				if(res && res.rows) {
					this.workOrderList = res.rows || [];
					this.total = res.total || 0;
					
					if (isRefresh) {
						// 直接赋值是无法触发 vue更新 的，需要this.$set方式才会触发
						// this.workOrderDoubleList[item.index] = res.rows || [];
						this.$set(this.workOrderDoubleList, item.index, res.rows || []);
					} else {
						const preWorkOrderList = this.workOrderDoubleList[item.index];
						if (!(preWorkOrderList && preWorkOrderList.length > 0)) {
							this.workOrderDoubleList[item.index] = res.rows || [];
						}
					}
					console.log("workOrderDoubleList", this.workOrderDoubleList);
				}
				
				let footer = res.footer || [];
				if (footer.length >0) {
					this.workorderNum = footer[0].workorderNum;
					this.hasCompletionNum = footer[0].hasCompletionNum;
				} else {
					for (let obj of this.workOrderList) {
						this.workorderNum += obj.workorderNum ? obj.workorderNum : 0;
						this.hasCompletionNum += obj.hasCompletionNum ? obj.hasCompletionNum : 0;
					}
				}
				this.loadingShow = false;
			}).catch(err => {
				console.error("查询待报工的订单数据异常:", err);
				this.loadingShow = false;
			});
		},
		tabClick(e) {
			console.log("tabClick,currentTabIndex", e, this.currentTabIndex);
			const preIndex = this.currentTabIndex;
			this.currentTabIndex = e.index;
			if (preIndex != e.index) {
				this.queryWaitingCompletionList(e);
			}
			this.station = e;
			this.curStationOverflowSubmitFlag = this.tabList[e.index].overflowSubmitFlag;
		},
		floatOnclick(e) {
			// TODO 扫描按钮事件
			console.log("扫描按钮事件:", e);
			// alert("正在打开飞书摄像头扫码");
			let _this = this;
			window.h5sdk.ready(() => {
				tt.scanCode({
					scanType: ["barCode", "qrCode", "datamatrix", "pdf417"],
					barCodeInput: true,
					success(res) {
						console.log("扫描返回:", res);
						// alert("成功:"+JSON.stringify(res));
						_this.contractCode = res.result;
						_this.searchWaitingCompletionList();
					},
					fail(res) {
						console.log(`scanCode fail:`, res);
						alert("失败:"+JSON.stringify(res));
					},
				});
			});
		},
		calendarConfirm(selectedItem) {
			console.log("日历选中", selectedItem);
			this.submitDateList = selectedItem;
			this.showDate = false;
			this.defaultDateMultiple = selectedItem;
			this.queryDateBtnText = "清空查询日期";
			this.searchWaitingCompletionList();
		},
		calendarClose() {
			this.showDate = false;
		},
		open() {
			// console.log('open');
			document.body.style.overflow = 'hidden'; // 禁用背景页面滚动
		},
		close() {
			this.show = false
			// console.log('close');
			document.body.style.overflow = ''; // 恢复背景页面滚动
		},
		reset() {
		},
		trigger(e) {
			console.log("flowButton.e, multiCollaspe", e, this.multiCollaspe);
			const btn = this.content[e.index];
			btn.active = true;
			if (btn.text === '刷新当前') {
				const selectedItem = this.tabList[this.currentTabIndex];
				this.queryWaitingCompletionList(selectedItem, true);
				btn.active = false;
			} else if (btn.text === '提交报工') {
				this.submitCompletion();
			} else if (btn.text === '返回上页') {
				this.navigateTo("/pages/work/dayreport/worksubmitViewB");
			} else if (btn.text === '工单历史') {
				const contractCode = encodeURIComponent(this.contractCode);
				const workerorderCode = encodeURIComponent(this.workerorderCode);
				const url = `/pages/work/dayreport/worksubmitSelfHistory?factoryId=${this.factoryId}&teamId=${this.teamId}&contractCode=${contractCode}&workerorderCode=${workerorderCode}`;
				this.navigateTo(url);
			}
		},
		navigateTo(url) {
		  uni.navigateTo({ url: url });
		},
		submitCompletion() {
			const submitWorkOrderList = this.workOrderDoubleList[this.currentTabIndex];
			if (!submitWorkOrderList || submitWorkOrderList.length == 0) {
				this.$refs.uToast.error(`报工数据为空，提交失败`);
				return;
			}
			// 开始计算进行提交数据是否存在异常
			this.completionWorkList = [];
			let total = 0;
			let unGoodTotal = 0;
			for(let item of submitWorkOrderList) {
				if (!item.todayCompletionReq && Number(item.todayCompletionReq) <= 0
					&& !item.ungoodNumReq && Number(item.ungoodNumReq) <= 0) {
					continue;
				}
				this.completionWorkList.push({
					contractCode: item.contractCode,
					bomName: item.bomName,
					bomStandard: item.bomStandard,
					workerorderId: item.workerorderId,
					workerorderCode: item.workerorderCode,
					bomCode: item.bomCode,
					todayCompletionReq: item.todayCompletionReq,
					ungoodNumReq: item.ungoodNumReq,
					remark: item.remark,
				});
				total += Number(item.todayCompletionReq);
				unGoodTotal += Number(item.ungoodNumReq);
			}
			if (this.completionWorkList.length == 0) {
				this.$refs.uToast.error(`今日报工数都是 0，请先输入`);
				return;
			}
			
			this.submitTotal = total;//  合格产品
			this.submitUngoodTotal = unGoodTotal;// 不良品
			this.submitConfirmShow = true;
			console.log('提交报工数据：', this.completionWorkList);
		},
		submitReally() {
			let station = this.tabList[this.currentTabIndex];
			for(let item of this.completionWorkList) {
				item.stationId = station.stationId;
				item.stationName = station.stationName;
				if (this.errMessage) {
					if (item.remark) {
						item.remark = item.remark + "," + this.errMessage;
					} else {
						item.remark = this.errMessage;
					}
				}
				item.submitFinishDate = this.submitFinishDate;
			}
			console.log("报工真正提交:", this.completionWorkList);
			submitApi.newSubmitStationDay(this.completionWorkList).then(res => {
				// 提交结果
				console.log("报工真正提交成功",  res);
				if (res.code == 200) {
					this.$refs.uToast.success(`今日报工提交成功,完工数:${this.submitTotal}`);
					this.searchWaitingCompletionList();
					this.submitConfirmShow = false;
					this.errMessage = null;
				} else {
					this.$refs.uToast.error(`今日报工提交失败,${res.msg}`);
				}
			}).catch(err => {
				// 提交异常
				console.error("报工真正提交发生异常", err);
			});
		},
		goToPre() {
			console.log("返回主页");
			uni.reLaunch({ url: '/pages/work/index' });
		},
	}
}
</script>

<style lang="scss" scoped>
/deep/.u-cell__body {
  // padding: 6px 0 !important;
  display: flex;
  flex-direction: row;
  box-sizing: border-box;
  padding: 6px 8px !important;
  font-size: 15px;
  color: #303133;
  align-items: center;
}
.card-container{
	width: 100%;
	min-height: 100%;
	box-sizing: border-box;
	padding: 10rpx;
	background-color: #ECECEC;
}
.card-wrap{
	padding: 12rpx;
	border-radius: 12rpx;
	margin-bottom: 140rpx;
	background-color: #FFF;
	border: 1px solid #ddd; /* 默认边框 */
	transition: background-color 0.3s, border-color 0.3s;
	.card-item{
		display: flex;
		align-items: center;
		font-size: 28rpx;
		padding-left: 6rpx;
		padding-bottom: 18rpx;
		color: #40485d;
	}
	.card-item-txt{
		flex:1;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
}
.card-btns{
	display: flex;
	justify-content: space-between;
	align-items: center;
}
.card-btns uni-button {
  flex: 1;
  margin: 0 5px; /* 调整按钮间距 */
}
.card-wrap-selected {
  border-color: #007bff; /* 选中状态的边框颜色 */
  background-color: #e7f0ff; /* 选中状态的背景颜色 */
}
.gridcell {
  border: 1px solid #ddd;
  text-align: center;
  line-height: 20px; /* Adjust line-height to match height */
  height: 30px; /* Set height to 20px */
  background-color: #f9f9f9;
  font-size: 14px; /* Adjust font-size to fit */
  overflow: hidden; /* Hide overflow if text is too large */
}
.card-item-single{
	border-radius: 6rpx;
	background-color: #FFF;
	border: 1px solid #ddd; /* 默认边框 */
	transition: background-color 0.3s, border-color 0.3s;
	align-items: left;
	font-size: 30rpx;
	color: #40485d;
}

.submit {
	// background-color: #007cff;
	text-align: center;
	margin: 5px 15px 5px 5px;
}

.demo-layout {
	height: 25px;
	border-radius: 4px;
}
.bg-purple {
	background: #CED7E1;
}
.bg-purple-light {
	background: #e5e9f2;
}


.flex-item {
	width: 33.3%;
	height: 200rpx;
	text-align: center;
	line-height: 200rpx;
}

.flex-item-V {
	width: 100%;
	height: 150rpx;
	text-align: center;
	line-height: 150rpx;
}

.text {
	margin: 15rpx 10rpx;
	padding: 0 20rpx;
	background-color: #ebebeb;
	height: 70rpx;
	line-height: 70rpx;
	text-align: center;
	color: #777;
	font-size: 26rpx;
}

.desc {
	/* text-indent: 40rpx; */
}
.flex-pc {
	display: flex;
	justify-content: center;
}

</style>
